package com.jy.appmarket.view;

import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

public class ProgressTextView extends TextView {

	private LinearGradient mLinearGradient;
	private Paint mPaint;
	private int mViewWidth = 0;
	private float mProgress;
	private int[] PROGRESS_COLOR = new int[] { 0xffffffff, 0xffffffff, 0xff00acff, 0xff00acff };

	public void setProgress(float progress) {
		mProgress = progress;
		mPaint = getPaint();
		mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, PROGRESS_COLOR, new float[] { 0,
				mProgress, mProgress, 1 }, Shader.TileMode.CLAMP);
		mPaint.setShader(mLinearGradient);
		invalidate();
	}

	public ProgressTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if (mViewWidth == 0) {
			mViewWidth = getMeasuredWidth();
			if (mViewWidth > 0) {
				mPaint = getPaint();
				mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, PROGRESS_COLOR,
						new float[] { 0, mProgress, mProgress, 1 }, Shader.TileMode.CLAMP);
				mPaint.setShader(mLinearGradient);
			}
		}
	}

	public void setProgressTextColorRes(int colorId) {
		setProgressTextColor(getResources().getColor(colorId));
	}

	public void setProgressTextColor(int color) {
		PROGRESS_COLOR[2] = color;
		PROGRESS_COLOR[3] = color;
		mPaint = getPaint();
		mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, PROGRESS_COLOR, new float[] { 0,
				mProgress, mProgress, 1 }, Shader.TileMode.CLAMP);
		mPaint.setShader(mLinearGradient);
		invalidate();
	}
}